//
// Created on 2025/8/23.
//
// Node APIs are not fully supported. To solve the compilation error of the interface cannot be found,
// please include "napi/native_api.h".

#include "md_vr_library.h"
#include "md_log.h"
#include "md_defines.h"
#include "md_renderer.h"
#include <native_window/external_window.h>
#include <sstream>
#include "device/md_nativewindow_ref.h"

namespace asha {
namespace vrlib {


class MDVRLibraryOH : public MDVRLibraryAPI {
public:
    int SetSurfaceId(std::string surface_id) override {
        MD_LOGI("MDVRLibraryOH::SetSurfaceId:%s", surface_id.c_str());
        // // 根据SurfaceId创建NativeWindow，注意创建出来的NativeWindow在使用结束后需要主动调用OH_NativeWindow_DestoryNativeWindow进行释放。
        if (!surface_id.empty()) {
            uint64_t surface_id_ptr = 0;
            std::istringstream iss(surface_id);
            iss >> surface_id_ptr;
            OHNativeWindow* window = nullptr;
            int ret = OH_NativeWindow_CreateNativeWindowFromSurfaceId(surface_id_ptr, &window);
            if (ret == 0) {
                std::shared_ptr<MDNativeWindowRef> ref = std::make_shared<MDNativeWindowRef>(window);
                OH_NativeWindow_DestroyNativeWindow(window);
                renderer_->SetSurface(ref);
            }
            return ret;
        } else {
            renderer_->SetSurface(nullptr);
            return MD_OK;
        }
    }
    
    virtual void UpdateMVPMatrix(float* matrix) override {
        renderer_->UpdateMVPMatrix(matrix);
    }
    
    virtual int Init() override {
        return renderer_->Init();
    }
    
    virtual int Destroy() override {
        return renderer_->Destroy();
    }
    
    virtual int Resume() override {
        return renderer_->Resume();
    }
    
    virtual int Pause() override {
        return renderer_->Pause();
    }
    
    virtual uint64_t GetVideoSurfaceId() override {
        return renderer_->GetVideoSurfaceId();
    }
    
    virtual void SetClearColor(float r, float g, float b, float a) override {
        MD_LOGI("MDVRLibraryOH::SetClearColor called: r=%.3f, g=%.3f, b=%.3f, a=%.3f", r, g, b, a);
        renderer_->SetClearColor(r, g, b, a);
    }
    
    virtual void SetCullFaceEnabled(bool enabled) override {
        MD_LOGI("MDVRLibraryOH::SetCullFaceEnabled called: enabled=%s", enabled ? "true" : "false");
        renderer_->SetCullFaceEnabled(enabled);
    }
    
    virtual void SetDepthTestEnabled(bool enabled) override {
        MD_LOGI("MDVRLibraryOH::SetDepthTestEnabled called: enabled=%s", enabled ? "true" : "false");
        renderer_->SetDepthTestEnabled(enabled);
    }
   
private:
    std::shared_ptr<MD360RendererAPI> renderer_ = MD360RendererAPI::CreateRenderer();
};

std::shared_ptr<MDVRLibraryAPI> MDVRLibraryAPI::CreateLibrary() {
    auto library = std::make_shared<MDVRLibraryOH>();
    return library;
} 



}
};

